package com.tsingteng.cosfun.ui.search;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.SearchUserResp;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class SearchFanAdapter extends BaseAdapter{
    private Context mContext;
    private List<SearchUserResp> mListRecommen;
    private SearchFanListener listener;
    public SearchFanAdapter(Context context,SearchFanListener mListener){
        this.mContext = context;
        this.mListRecommen = new ArrayList<>();
        this.listener = mListener;
    }
    @Override
    public int getCount() {
        return mListRecommen.size();
    }

    @Override
    public SearchUserResp getItem(int i) {
        return mListRecommen.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view ==null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_fans_search,viewGroup,false);
            viewHolder.mIvFansHeader = view.findViewById(R.id.iv_fans_header);
            viewHolder.mTvFansTitle = view.findViewById(R.id.tv_fans_title);
            viewHolder.mTvFansId = view.findViewById(R.id.tv_fans_id);
            viewHolder.mFanNumTv = view.findViewById(R.id.fan_num_tv);
            viewHolder.mFanWorkTv = view.findViewById(R.id.fan_work_tv);
            viewHolder.mTvFansFollow = view.findViewById(R.id.tv_fans_follow);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.mTvFansTitle.setText(mListRecommen.get(i).getProfileName());
        viewHolder.mFanNumTv.setText(mListRecommen.get(i).getNumberOfFans()+mContext.getResources().getString(R.string.fans));
        viewHolder.mFanWorkTv.setText(mListRecommen.get(i).getNumberOfWorks()+mContext.getResources().getString(R.string.commonet));
        viewHolder.mTvFansId.setText(mContext.getResources().getString(R.string.profile_id)+mListRecommen.get(i).getProfileId());
        ImageUtils.LoadImage(mContext, mListRecommen.get(i).getProfileAvatar(), viewHolder.mIvFansHeader);
        if (1 == mListRecommen.get(i).getIsAttention()){
            viewHolder.mTvFansFollow.setText(mContext.getResources().getString(R.string.focused));
        }else if (0 == mListRecommen.get(i).getIsAttention()){
            viewHolder.mTvFansFollow.setText(mContext.getResources().getString(R.string.focus));
        }else if (2 == mListRecommen.get(i).getIsAttention()){
            viewHolder.mTvFansFollow.setText(mContext.getResources().getString(R.string.focus_each));
        }
        viewHolder.mTvFansFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.searchFanListener(i,mListRecommen.get(i));
            }
        });
        viewHolder.mIvFansHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("user_id",mListRecommen.get(i).getProfileId());
                bundle.putInt(Constant.TAG, 1);
                TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
            }
        });
        return view;
    }
    class ViewHolder{
       CircleImageView mIvFansHeader;
       TextView mTvFansTitle;
       TextView mTvFansId;
       TextView mFanNumTv;
       TextView mFanWorkTv;
       TextView mTvFansFollow;
    }
    public void refreshListFan(List<SearchUserResp> listRecommen){
        mListRecommen.clear();
        mListRecommen.addAll(listRecommen);
        notifyDataSetChanged();
    }
    public void addListFan(List<SearchUserResp> listRecommen){
        mListRecommen.addAll(listRecommen);
        notifyDataSetChanged();
    }
    interface SearchFanListener{
        void searchFanListener(int position,SearchUserResp listResp);
    }
}
